#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:

    int LastRemaining_Solution(int n, int m) {
        int dp = 0;
        for (int i = 1; i < n; i++)
            dp = (dp + m) % (i + 1);
        return dp;
    }
};